﻿using IM.Easy.Core;
using IM.Easy.Core.Configuration;
using IM.Easy.Core.Options;
using IM.Easy.Core.Security;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.DependencyInjection;

namespace IM.Easy.Core.AppContext
{
    public static class ContextExtensions
    {
        public static IApplicationBuilder ConfigureAppContext(this IApplicationBuilder app)
        {
            var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
            ImApp.Configure(httpContextAccessor);
            var options = ImApp.Configuration.Get<JwtOptions>();
            JwtEncryption.Configure(options);
            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });

            return app;
        }
    }
}
